﻿var TopLayoutSettings = {
    defaults: {
        size: "auto"
		, minSize: 5
        , resizable: false
		, paneClass: "ui-layout-pane"
		, resizerClass: "ui-layout-resizer"
		, togglerClass: "ui-layout-toggler"
		, buttonClass: "ui-layout-button"
		, contentSelector: ".content"	// inner div to auto-size so only it scrolls, not the entire pane!
		, contentIgnoreSelector: "span"		// 'paneSelector' for content to 'ignore' when measuring room for content
		, togglerLength_open: 35			// WIDTH of toggler on north/south edges - HEIGHT on east/west edges
		, togglerLength_closed: 35			// "100%" OR -1 = full height
        , spacing_open: 0
        , spacing_closed: 0
		, hideTogglerOnSlide: true		// hide the toggler when pane is 'slid open'
		, togglerTip_open: "关闭面板"
		, togglerTip_closed: "打开面板"
		, resizerTip: "调节面板尺寸"
		, slidable: false
        //	effect defaults - overridden on some panes
		, fxName: "slide"		// none, slide, drop, scale
		, fxSpeed_open: 750
		, fxSpeed_close: 1500
		, fxSettings_open: { easing: "easeInQuint" }
		, fxSettings_close: { easing: "easeOutQuint" }
    }
    , west: {
        size: 250
        , spacing_open: 5
        , spacing_closed: 5
    }
    , center: {

}
};

function ajax(method, param, success) {
    $.ajax({
        url: "../dispatchsystem/proxy.aspx?m=" + method
        , dataType: "json"
        , type: "post"
        , data: param
        , success: function (data) {
            if (!AjaxError(data)) {
                success(data);
            }
        }
        , error: function (ret) {
            alert(ret.responseText);
        }
    });
};

var currentDistrictId = -1;
var start = "", end = "";
var currentParkId = null;
var currentPageSize = null;

function GetDistrictIcon(type) {
    return parkMSys.jsSiteHost + "/images/region.png";
};

function GetCollectorMark(type, status) {
    var img = parkMSys.jsSiteHost + "/images/c_" + type+"_" + status + ".png";
    var myIcon = new BMap.Icon(img, new BMap.Size(28, 31),
    {
        offset: new BMap.Size(14, 31),
        imageOffset: new BMap.Size(0, 0)
    });
    return myIcon;
}

function GetRepeaterMark(type, status) {
    var img = parkMSys.jsSiteHost + "/images/r_" + type + "_" + status + ".png";
    var myIcon = new BMap.Icon(img, new BMap.Size(14, 21),
    {
        offset: new BMap.Size(7, 21),
        imageOffset: new BMap.Size(0, 0)
    });
    return myIcon;
}

function init() {

    $("#btnSearch").click(function () {
        var key = $.trim($("#txtKey").val());
        if (key == "") {
            alert("查询前请输入关键字!");
            return;
        }
        ajax("searchDistrict", { name: encodeURIComponent(key) }, function (arr) {
            $("#dvDistrictListTitle").show().find("span.key").text(key).end().find("span.sum").text(arr.length);
            $("#dvDistrictList").empty().append("#distractTmpl", arr);
            map.clearOverlays();
        });
    });
};


$(function () {

    if (!IsOnLine()) {
        return;
    }
    $('body').layout(TopLayoutSettings);

    ajax("getInitLngLat", {}, function (obj) {
        initMap("mapC", obj.lng, obj.lat, obj.level);
    });

    ajax("getCityRegions", {}, function (arr) {
        $("#dvRegions").append("#regionsTmpl", arr);
    });

});

function ClickEquipment(type) {
    var mapbounds = map.getBounds();
    var p1 = mapbounds.getSouthWest();
    var p2 = mapbounds.getNorthEast();
    var m = "GetCollectorPlaceHolderRate";
    var typename = "";
    if ($("#ratetype").is(":checked")) {
        if (type == 1) {
            m = "GetCollectorPlaceHolderRate";
        }
        else {
            m = "GetRepeaterPlaceHolderRate";
        }
        typename = "rate";
    }
    else if ($("#illegaltype").is(":checked")) {
        if (type == 1) {
            m = "GetCollectorRuleBreak";
        }
        else {
            m = "GetRepeaterRuleBreak";
        }
        typename ="break";
    }
    else {
        if (type == 1) {
            m = "GetCollectorBug";
        }
        else {
            m = "GetRepeaterBug";
        }
        typename= "bug";
    }
    ajax(m, { lng0: p1.lng, lng1: p2.lng, lat0: p1.lat, lat1: p2.lat }, function (obj) {
        map.clearOverlays();
        var markPArr = [];
        $.each(obj.data, function (i, v) {
            var point = new BMap.Point(v.lng, v.lat);
            markPArr.push(point);
            var myIcon = null;
            if (type == 1) {
                myIcon = GetCollectorMark(typename, v.status);
            }
            else {
                myIcon = GetRepeaterMark(typename, v.status);
            }

            var marker = new BMap.Marker(point, { icon: myIcon });
            map.addOverlay(marker);

            marker.addEventListener("click", function () {
                var that = this;
                var m2 = "ClickCollector";
                if (type != 1) {
                    m2 = "ClickRepeater";
                }
                ajax(m2, { id: v.id }, function (obj) {
                    obj.id = v.id;
                    obj.equipType = type;
                    var html = $("#tmplMarkInfo").tmpl(obj);
                    var opts = {
                        width: 300,
                        height: 320,
                        offset: new BMap.Size(0, 0),
                        title: obj.address
                    }
                    var infoWindow = new BMap.InfoWindow(html[0].outerHTML, opts);
                    that.openInfoWindow(infoWindow);
                    currentMarker = that;
                });
            });
        });
        map.setViewport(markPArr);
    });
}

function GetParkImg(type) {
    return parkMSys.jsSiteHost + "/images/park_" + type + ".png"; 
}

function GetParkDetail(equipType, id, pageSize, pageNumber) {
    var m = "getCollectorDetail";
    if (equipType != 1) {
        m = "getRepeaterDetail";
    }

    ajax(m, { id: id, size: pageSize, number: pageNumber }, function (obj) {


        $.each(obj.date, function (i, v) {
            v.parkImageUrl = GetParkImg(v.type); // 各种小车图片
        });

        // 测试用

        try {
            var o = obj.date[0];
            var n = 8 - obj.date.length;
            for (var i = 0; i < n; i++) {
                obj.date.push(o);
            }
        }
        catch (e1) { }

        // end

        var html = $("#tmplParkDetail").tmpl(obj);

        var content = html[0].outerHTML;

        var title = (equipType == 1 ? "收集器" : "中继器") + "编号：" + obj.number;

        var pagecount = obj.allRecordCount / pageSize;

        if (obj.allRecordCount % pageSize > 0) {
            pagecount++;
        }

        // 测试用
        pagecount = 100;
        // end

        var InfoWnd = map.getInfoWindow();

        if (InfoWnd == null || !InfoWnd.isOpen()) {
            return;
        }

        InfoWnd.setWidth(1000);
        InfoWnd.setHeight(1000);
        InfoWnd.setContent(content);
        InfoWnd.setTitle(title);

        $("div.pager").pager({ pagenumber: pageNumber, pagecount: pagecount, buttonClickCallback:
                    function (pageclickednumber) {
                        
                        GetParkDetail(equipType, id, pageSize, pageclickednumber);
                    }
        });

        start = "";
        end = "";

    });
    
}

function DistrictItemClick(item, districtId) {
    ajax("getDistrictScopeBorderData", { id: districtId }, function (arr) {
        var polyArr = [];
        $.each(arr, function (i, v) {
            polyArr.push(new BMap.Point(v.lng, v.lat));
        });
        var polygon = new BMap.Polygon(polyArr, { strokeColor: "blue", strokeWeight: 1, strokeOpacity: 0.5 });
        map.clearOverlays();
        map.addOverlay(polygon);
        map.setViewport(polyArr);
        var cp = map.getCenter();
        polygon.addEventListener("mouseover", function () {
            ajax("getDistrictScopeInfoData", { id: districtId }, function (obj) {

                var html = $("#tmplCityInfo").tmpl(obj);
                var opts = {
                    width: 350,
                    height: 240,
                    title: obj.title
                }
                var infoWindow = new BMap.InfoWindow(html[0].outerHTML, opts);
                map.openInfoWindow(infoWindow, cp);
                infoWindow.addEventListener("close", function () {
                    //                    map.panTo(cp);
                });
            });
        });

        polygon.addEventListener("mouseout", function () {
            map.closeInfoWindow();
        });
    })
};

function getDistrictListOfRegion(regionId, regionName) {
    $("#dvDistrictListTitle").show().find("span.key").text(regionName);
    ajax("getDistrictOfRegion", { id: regionId }, function (arr) {
        $("#dvDistrictListTitle").find("span.sum").text(arr.length);
        $("#dvDistrictList").empty().append("#distractTmpl", arr);
        getRegionScopeBorderData(regionId);
    });
};

function getRegionScopeBorderData(regionId) {
    ajax("getRegionScopeBorderData", { id: regionId }, function (arr) {
        var polyArr = [];
        $.each(arr, function (i, v) {
            polyArr.push(new BMap.Point(v.lng, v.lat));
        });
        var polygon = new BMap.Polygon(polyArr, { strokeColor: "blue", strokeWeight: 1, strokeOpacity: 0.5 });
        map.clearOverlays();
        map.addOverlay(polygon);
        map.setViewport(polyArr);
        var cp = map.getCenter();
        polygon.addEventListener("mouseover", function () {
            ajax("getRegionScopeInfoData", { id: regionId }, function (obj) {
                
                var html = $("#tmplCityInfo").tmpl(obj); 
                var opts = {
                    width: 350,
                    height: 240,
                    title: obj.title
                }
                var infoWindow = new BMap.InfoWindow(html[0].outerHTML, opts);
                map.openInfoWindow(infoWindow, cp);
                infoWindow.addEventListener("close", function () {
                    //                    map.panTo(cp);
                });
            });
        });

        polygon.addEventListener("mouseout", function () {
            map.closeInfoWindow();
        });
    });
};

var currentMarker = null;

function parkUserNotify(parkId, vehicleNumber) {
    ajax("parkUserNotify", { id: parkId, number: vehicleNumber }, function (obj) {
        if (obj.success == 1) {
            alert("提醒成功！");
        }
        else {
            alert("提醒失败！");
        }
    });
};

function listParkingOfPark(parkId, startTime, endTime, pagesize, pagenumber, first) {
    currentParkId = parkId;
    currentPageSize = pagesize;
    ajax("listParkingOfPark", { id: parkId, start: startTime, end: endTime, size: pagesize, number: pagenumber }, function (obj) {
        var html = $("#parkingTmpl").tmpl(obj);

        var content = html[0].outerHTML;
        var title = "停泊记录";

        var pagecount = obj.RowsCount / pagesize;
        if (obj.allRecordCount % pagesize > 0) {
            pagecount++;
        }

        var InfoWnd = map.getInfoWindow();

        if (InfoWnd == null || !InfoWnd.isOpen()) {
            return;
        }

        /*  baidu box 方式
        map.closeInfoWindow();
        var infoBox = new BMapLib.InfoBox(map, content, {
        boxStyle: {
        background: "#fff"
        , width: "800px"
        , height: "400px"
        }
        , closeIconMargin: "1px 1px 0 0"
        , enableAutoPan: true
        , align: INFOBOX_AT_TOP
        });
        
        infoBox.open(currentMarker);
        //        currentMarker.enableDragging();
        // end */


        //* 常规infoWindow
        InfoWnd.setWidth(1000);
        InfoWnd.setHeight(1000);
        InfoWnd.setContent(content);
        InfoWnd.setTitle(title);
        //*/

        $(".startDp").datetimepicker({ dateFormat: "yy-mm-dd", onClose: function () { start = $(".startDp").datetimepicker("getDate"); } });
        $(".endDp").datetimepicker({ dateFormat: "yy-mm-dd", onClose: function () { end = $(".endDp").datetimepicker("getDate"); } });

        $(".startDp").datetimepicker("setDate", start);
        $(".endDp").datetimepicker("setDate", end);

        $("div.pager").pager({ pagenumber: pagenumber, pagecount: pagecount, buttonClickCallback:
                    function (pageclickednumber) {//new Date().getUTCMinutes
                        var s1 = $(".startDp").val();
                        var e1 = $(".endDp").val();
                        listParkingOfPark(parkId, s1, e1, pagesize, pageclickednumber, false);
                    }
        });

    });
};

function queryParking() {
    var s1 = $(".startDp").val();
    var e1 = $(".endDp").val();
    listParkingOfPark(currentParkId, s1, e1, currentPageSize, 0, false);
};